
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.function.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.function.entity.Function;
import com.application.function.entity.RoleFunction;
import com.application.function.struts.form.FunctionForm;
import com.application.function.service.FunctionService;
import com.application.function.commons.Constants;
import com.application.role.entity.Role;
import com.application.usertable.entity.RoleUser;

/**
 * 
 * Description:FunctionAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class FunctionAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private FunctionService functionService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Function function;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getFunctionPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchFunction" + "开始");
		FunctionForm function = (FunctionForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List functionList = null;
		resultSize = functionService.getAllCountFunction(function);
		pageBean = PageBean.getPageBean("functionTab", resultSize, request);
		functionList = functionService.searchFunction(function,
				pageBean);
		request.setAttribute("functionList", functionList);

		log.debug("searchFunction" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddFunctions");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addFunctions");
		Function function = this.getFunction(form);
		String id = RandomKey.generateID_Long_19().toString();
		function.setId(id);
		function.setCreateDate(new Date());
		String roleId = request.getParameter("roleId");
		String[] roleIds = null;
		if(roleId!=null){
			roleIds = roleId.split(",");
		}
		this.functionService.insertObject(function);
		if(roleIds!=null){
			for(int i=0;i<roleIds.length;i++){
				if(!roleIds[i].equals("")){
				RoleFunction r = new RoleFunction();
				r.setId(RandomKey.generateID_Long_19().toString());
				r.setFid(id);
				r.setRid(roleIds[i]);
				r.setCreateDate(new Date());
				this.functionService.insertObject(r);
				}
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteFunctions");
		FunctionForm functionForm = (FunctionForm) form;
		this.functionService.deleteFunctions(functionForm);
		this.functionService.deleteFunctionRole(functionForm.getId());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateFunctions");
		FunctionForm functionForm = (FunctionForm) form;
		function = (Function) this.functionService.searchFunction(
				functionForm).get(0);
		List<Role> roleUser= this.functionService.getFunctionRole(function.getId());
		String roleIds="";
		String roleNames="";
		if(roleUser!=null&&roleUser.size()>0){
			for(int i=0;i<roleUser.size();i++){
				Role temp = roleUser.get(i);
				roleIds = temp.getId()+","+roleIds;
				roleNames = temp.getName()+" "+roleNames;
			}
			
		}
		request.setAttribute(Constants.ROLEIDS, roleIds);
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, function);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateFunctions");
		Function function = this.getFunction(form);
		function.setCreateDate(new Date());
		this.functionService.updateObject(function);
		this.functionService.deleteFunctionRole(function.getId());
		String roleId = request.getParameter("roleId");
		String[] roleIds = null;
		if(roleId!=null){
			roleIds = roleId.split(",");
		}
		if(roleIds!=null){
			for(int i=0;i<roleIds.length;i++){
				if(!roleIds[i].equals("")){
				RoleFunction r = new RoleFunction();
				r.setId(RandomKey.generateID_Long_19().toString());
				r.setFid(function.getId());
				r.setRid(roleIds[i]);
				r.setCreateDate(new Date());
				this.functionService.insertObject(r);
				}
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewFunction");
		FunctionForm functionForm = (FunctionForm) form;
		function = (Function) this.functionService.searchFunction(
				functionForm).get(0);
		List<Role> roleUser= this.functionService.getFunctionRole(function.getId());
		String roleIds="";
		String roleNames="";
		if(roleUser!=null&&roleUser.size()>0){
			for(int i=0;i<roleUser.size();i++){
				Role temp = roleUser.get(i);
				roleIds = temp.getId()+","+roleIds;
				roleNames = temp.getName()+" "+roleNames;
			}
			
		}
		request.setAttribute(Constants.ROLEIDS, roleIds);
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, function);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the FunctionService
	 */
	public FunctionService getFunctionService() {
		return this.functionService;
	}

	/**
	 * @param FunctionService
	 *            the FunctionService to set
	 */
	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	/**
	 * 获取Function实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Function
	 */
	public Function getFunction(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		FunctionForm functionForm = (FunctionForm) form;
		function = new Function();
		BeanUtils.copyProperties(function, functionForm);
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

//	public ActionForward creatTreeSelect(ActionMapping mapping,
//			ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) throws IOException {
//		response.setContentType("text/text;charset=UTF-8");
//		// System.out.println(request.getParameter("pid"));
//		FunctionForm functionForm = new FunctionForm();
//		functionForm.setFid(request.getParameter("pid"));
//		String vid = request.getParameter("treeid");
//		int tjs = Integer.parseInt(request.getParameter("js"));
//		Long pid = Long.parseLong(request.getParameter("pid"));
//		List<Function> accList = functionService
//				.searchFunction(functionForm);
//		String html = creatTreeSelect(accList, vid, tjs);
//		java.io.PrintWriter out = response.getWriter();
//		out.print(html);
//		out.flush();
//		out.close();
//		return null;
//	}
//
//	public String creatTreeSelect(List<Function> accList, String vid, int js) {
//		String html = "";
//		for (int i = 0; i < accList.size(); i++) {
//			Function function = (Function) accList.get(i);
//			for (int j = 0; j < js; j++) {
//				html = html + "<img src='images/line.gif'>";
//			}
//			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
//					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + function.getId() + "');\" >"
//					+ "<img src='images/close1.gif' id='img" + vid + js + i
//					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + function.getId()
//					+ "');\" style=\"cursor:hand\">&nbsp;";
//
//			html = html + "<a onclick=\"selectTree('" + function.getId()
//					+ "','" + function.getName()
//					+ "')\" style=\"cursor:hand\">" + function.getName()
//					+ "</a>";
//			html = html + "<div id='" + vid + js + i
//					+ "' ><span class='f1'>.</span></div>";
//		}
//		return html;
//	}

}
